package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.api.pay.PayClientApi;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.pay.pojo.ApUserVip;
import com.zhentao.model.user.dto.UserPageDto;
import com.zhentao.model.user.pojo.ApUser;
import com.zhentao.model.user.pojo.ApUserInfo;
import com.zhentao.service.ApUserService;
import com.zhentao.mapper.ApUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 许文倩
* @description 针对表【ap_user】的数据库操作Service实现
* @createDate 2024-07-06 10:53:35
*/
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser>
    implements ApUserService{
    @Resource
    ApUserMapper apUserMapper;
    @Resource
    PayClientApi payClientApi;

    @Override
    public ResponseResult getlist(UserPageDto dto) {
        System.out.println(dto.getPageNum()+"-------------------"+dto.getPageSize()+"");
        Page<ApUser> page=new Page(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<ApUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(dto.getNickName()!=null,ApUser::getNickName,dto.getNickName());
        page = apUserMapper.selectPage(page,wrapper);
        System.err.println(page.getTotal());
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPageNum(), dto.getPageSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setCode(200);
        pageResponseResult.setErrorMessage("查询成功");
        return pageResponseResult;
    }

    @Override
    public ResponseResult addUser(ApUser apUser) {
        QueryWrapper<ApUser> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(apUser.getOpenid()!=null,ApUser::getOpenid,apUser.getOpenid());
        ApUser apUser1 = apUserMapper.selectOne(wrapper);
        if(apUser1!=null){
            return ResponseResult.okResult(apUser1);
        }
        apUser.setIsReal(0);
        apUser.setStatus(1);
        apUserMapper.insert(apUser);
        return ResponseResult.okResult("上传成功，待审核");
    }


    @Override
    public ResponseResult updateUser(ApUser apUser) {
        apUserMapper.updateById(apUser);
        return ResponseResult.okResult("更新成功，待审核");
    }

    @Override
    public ResponseResult selectMine() {
        QueryWrapper<ApUser> wrapper=new QueryWrapper();
        wrapper.lambda().eq(ApUser::getIsReal,1);
        List<ApUser> apUsers = apUserMapper.selectList(wrapper);

        return ResponseResult.okResult(apUsers);
    }

    @Override
    public ResponseResult ifUse(int id) {
        ApUser apUser = apUserMapper.selectById(id);
        if (apUser==null){
            return ResponseResult.errorResult(400,"未找到用户账号");
        }
        apUser.setStatus(apUser.getStatus()==0 ? 1:0);
        if (apUser.getStatus()==1){
            apUser.setCreatedTime(new Date());
        }
        apUserMapper.updateById(apUser);
        return ResponseResult.okResult("状态修改成功");
    }

    @Override
    public ResponseResult userNotUpOne() {
        QueryWrapper<ApUser> wrapper=new QueryWrapper();
        wrapper.lambda().eq(ApUser::getIsReal,0);
        List<ApUser> apUsers = apUserMapper.selectList(wrapper);

        return ResponseResult.okResult(apUsers);
    }

    @Override
    public ResponseResult getByUserId(String userId) {
        QueryWrapper<ApUser> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(ApUser::getId,userId);
        ApUser apUser = apUserMapper.selectOne(wrapper);
        QueryWrapper<ApUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUser::getId,apUser.getReferrerId());
        ApUser apUser1 = apUserMapper.selectOne(queryWrapper);
        apUser.setReferrerName(apUser1.getNickName());
        ApUserVip apUserVip = payClientApi.selectOne(apUser.getId());
        apUser.setPastTime(apUserVip.getPastTime());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        return ResponseResult.okResult(apUser);
    }

    @Override
    public ResponseResult selectListByUser(Integer id) {
        QueryWrapper<ApUser> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(ApUser::getReferrerId,id);
        List<ApUser> users = apUserMapper.selectList(wrapper);
        return ResponseResult.okResult(users);
    }

}




